﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.BLL;

namespace ShopOnline
{
    public partial class ProductMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetCartTitle(string.Format("You have <a href='Cart.aspx'>{0} item(s)</a> in your shopping cart.", BasePage.SelectedItems.RowCount));
            if (!IsPostBack)
            {
                LoadTags();
                dailySpecial.Rebind();
                relLink.Rebind();
                LoadBanner(((BasePage)this.Page).BannerKey);
            }
        }

        protected void relLink_OnNeedDataSource(object sender, EventArgs e)
        {
            RelatedLinks rels = new RelatedLinks();
            rels.Query.Top = 5;
            rels.Query.Load();

            relLink.DataSource = rels.DefaultView;
        }

        public void LoadBanner(string key)
        {
            PageBanners banner = PageBanners.GetBanner(key);
            if (banner.RowCount > 0)
            {
                topBanner.ImageUrl = POSSystem.BLL.Utilities.GetConfigAppSetting("BannerArchive") + "/" + banner.FileName;
                topBanner.ToolTip = banner.Title;
                linkBanner.NavigateUrl = banner.URL;
            }
        }

        public void SetPageTitle(string title)
        {
            ((GeneralMaster)this.Master).SetPageTitle(title);
            lblPageTitle.Text = title;
        }

        public void SetCartTitle(string title)
        {
            lblCart.Text = title;
        }

        protected void dailySpecial_OnNeedDataSource(object sender, EventArgs e)
        {
            View_WebProducts p = View_WebProducts.LoadDailySpecial(3);
            dailySpecial.DataSource = p.DefaultView;
        }

        protected void LoadTags()
        {
            View_ProductTags tags = new View_ProductTags();
            tags.Query.AddOrderBy(View_ProductTags.ColumnNames.Count, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
            tags.Query.Top = 10;
            tags.Query.Load();

            string html = "";
            int[] fontSize = new int[]{145, 100, 75};
            int j = 0;

            List<string> words = new List<string>();

            for (int i = 0; i < tags.RowCount; i++)
            {
                words.Add(string.Format("<li><a style='font-size:{1}%' href='Search.aspx?tag={0}'>{0}</a></li>", tags.s_Tag, fontSize[j]));
                if (i % fontSize.Length == 0 && j < fontSize.Length)
                    j++;

                tags.MoveNext();
            }
            Random rnd = new Random();
            while (words.Count > 0)
            {
                int num = rnd.Next(words.Count);
                html += words[num];
                words.RemoveAt(num);
            }

            litTags.Text = html;
        }
    }
}